package com.c.ctgproject.adapter


import android.content.Intent
import android.os.Environment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import androidx.fragment.app.FragmentActivity
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.c.ctgproject.R
import com.c.ctgproject.customview.ImagePickerLoader
import com.c.ctgproject.entity.OldPhotoVideoEntity
import com.c.ctgproject.utils.MyLog
import com.c.ctgproject.wiget.dialog.ShowImgVideoFragment
import com.lwkandroid.imagepicker.ImagePicker
import com.lwkandroid.imagepicker.data.ImageContants
import com.lwkandroid.imagepicker.data.ImagePickType
import com.lwkandroid.imagepicker.data.ImagePickerOptions
import com.lwkandroid.imagepicker.ui.grid.view.ImageDataActivity


class PhotoShowListAdapter(
    private val context: FragmentActivity,
    private val mList: ArrayList<String>,
    private val max: Int,
    private val listener: DeleteCallBack,
    private val isEditable: Boolean,
    private val parentPosition: Int
) : RecyclerView.Adapter<PhotoShowListAdapter.MyViewHolder>() {


    fun handlerData(holder: RecyclerView.ViewHolder, position: Int) {
        MyLog.d(mList.size)
        if (holder is MyViewHolder) {
            if (isEditable) {
                if (position < mList.size) {
                    Glide.with(context).load(mList[position]).into(holder.mImageView)
                    holder.mDeleteIV.setOnClickListener {
//                        listener.deleteItem(mList[position], parentPosition)
                        mList.removeAt(position)
                        notifyDataSetChanged()
                    }
                    holder.mDeleteIV.visibility = View.VISIBLE
                    holder.mImageView.setOnClickListener {

                        ShowImgVideoFragment(
                            mList[position],
                            false
                        ).show(context.supportFragmentManager, "")

                    }
                } else {
                    Glide.with(context).load(R.drawable.ic_ngv_add_pic).into(holder.mImageView)
                    holder.mImageView.setOnClickListener {

                        val intent = Intent(context, ImageDataActivity::class.java)
                        val mOptions = ImagePickerOptions()
                        mOptions.isNeedCamera=true
                        mOptions.maxNum = max-mList.size
                        mOptions.type=ImagePickType.MULTI
                        mOptions.cachePath=Environment.getExternalStorageDirectory().absolutePath
                        intent.putExtra(ImageContants.INTENT_KEY_OPTIONS, mOptions)
                        context.startActivityForResult(intent, parentPosition)
                    }
                    holder.mDeleteIV.visibility = View.INVISIBLE
                }
            } else {
                if (position < mList.size) {
                    Glide.with(context).load(mList[position]).into(holder.mImageView)

                    holder.mDeleteIV.setOnClickListener {
                        listener.deleteItem(mList[position], parentPosition)
                        notifyDataSetChanged()
                    }
                    holder.mDeleteIV.visibility = View.INVISIBLE
                    holder.mImageView.setOnClickListener {

                        ShowImgVideoFragment(
                            mList[position],
                            false
                        ).show(context.supportFragmentManager, "")

                    }
                } else {
//                    Glide.with(context).load(R.mipmap.bg_add_photo_grey).into(holder.mImageView)
//                    holder.mImageView.setOnClickListener {
//                        context.startActivity<TakePhotoToDealActivity>()
//                    }

                    holder.mDeleteIV.visibility = View.GONE
                    holder.mImageView.visibility = View.GONE
                }
            }
//            holder.mImageView.setOnClickListener {
//                if(position<mList.size-1){
//                    ShowImgVideoFragment(mList[position],false).show(context.supportFragmentManager, "")
//                }
//            }
        }
    }

//    fun takePhoto() {
//        EasyPhotos.createCamera(context, true) //参数说明：上下文
//            .setFileProviderAuthority("com.android.ctgapp.fileprovider")
//            .start(object : SelectCallback() {
//                override fun onResult(photos: ArrayList<Photo>, isOriginal: Boolean) {
//                    //获取file,进行对应操作
//                    val file = File(photos[0].path)
//                    startForDeal(file)
//                }
//
//                override fun onCancel() {}
//            })
//    }
//
//    fun choosePhoto() {
//        EasyPhotos.createAlbum(context, false, true, GlideEngine.getInstance())
//            .setCount(max - mList.size)
//            .start(object : SelectCallback() {
//                override fun onResult(photos: ArrayList<Photo>, isOriginal: Boolean) {
//                    //获取file，进行对应操作
//                    //val file = File(photos[0].path)
//                    val photoPaths = ArrayList<String>().apply {
//                        photos.forEach {
//                            this.add(it.path)
//                        }
//                    }
//                    listener.choosePhotoItem(photoPaths, parentPosition)
//                    //MyLog.d(file.absolutePath)
//                }
//
//                override fun onCancel() {}
//            })
//
//    }

//    fun startForDeal(file: File) {
//        val intent = Intent(context, IMGEditActivity::class.java)
//        val uri = Uri.fromFile(file);
//        //mImageFile = File(context.getExternalCacheDir()!!.getPath(), UUID.randomUUID().toString() + ".jpg")
//        intent.putExtra(IMGEditActivity.EXTRA_IMAGE_URI, uri)
//        intent.putExtra(IMGEditActivity.EXTRA_IMAGE_SAVE_PATH, file.absolutePath)
//        intent.putExtra(IMGEditActivity.EXTRA_IMAGE_CONFORM_TITLE, "完成")
//        intent.putExtra("parentPosition", parentPosition)
//        //val currentChartActivity:CurrentChartActivity=context as CurrentChartActivity
//        //currentChartActivity.test(intent)
//        context.startActivityForResult(
//            intent,
//            1
//        )
//    }

    inner class MyViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        val mImageView: ImageView = view.findViewById(R.id.mImageView)
        val mDeleteIV: ImageView = view.findViewById(R.id.mDeleteIV)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
        val view =
            LayoutInflater.from(context).inflate(R.layout.item_show_photo_list, parent, false)
        return MyViewHolder(view)
    }

    override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
        handlerData(holder, position)
    }

    override fun getItemCount(): Int {
        if (max == mList.size) {
            return mList.size
        } else {
            return mList.size + 1
        }
    }

    interface DeleteCallBack {
        fun deleteItem(url: String, position: Int)
    }


}